
 /**
 * 上海中赢金融信息服务有限公司
 * Copyright (c) 2017-2027 Chinazyjr,Inc.All Rights Reserved.
 */
package com.sys.midware.redis.core.operation.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.log4j.Logger;

import com.sys.midware.redis.core.RedisFactory;
import com.sys.midware.redis.core.operation.AbstractOperations;
import com.sys.midware.redis.core.operation.StringsOperations;
import com.sys.midware.redis.exceptions.BRedisException;

import redis.clients.jedis.JedisCluster;

/**
 * <b>Description：</b> String 类型操作类 <br/>
 * <b>ClassName：</b> StringsOperationsImpl <br/>
 * <b>@author：</b> jackyshang <br/>
 * <b>@date：</b> 2016年8月22日 下午4:11:23 <br/>
 * <b>@version: </b>  <br/>
 */
public class ClusterStringsOperationsImpl extends AbstractOperations implements StringsOperations{
    private static Logger logger = Logger.getLogger(ClusterStringsOperationsImpl.class);
    private ClusterStringsOperationsImpl() {
    };

    private static ClusterStringsOperationsImpl instance;
    
    /**
     * 获取单例工厂
     * @return
     * @throws Exception
     */
    public static ClusterStringsOperationsImpl getInstance() {
        if (instance != null)
            return instance;

        synchronized (ClusterStringsOperationsImpl.class) {
            if (instance == null) {
                instance = new ClusterStringsOperationsImpl();
            }
            return instance;
        }
    }
    
    @Override
    public void save(String key, String value) {
        JedisCluster jedis = getClient4Cluster();
      try {
          getClient().set(key, value);
//          getClient().expire(key, expireTimeSeconds);
      } catch (Exception e) {
          throw new BRedisException(e);
      } finally {
          RedisFactory.close(jedis);
      }        
    }

    @Override
    public String get(String key) {
        JedisCluster jedis = getClient4Cluster();
     try {
         return jedis.get(key);
     } catch (Exception e) {
         logger.error(e);
         throw new BRedisException("redis 获取数据出错!", e);

     } finally {
         RedisFactory.close(jedis);
     }
    }

    @Override
    public boolean delete(String... keys) {
        JedisCluster jedis = getClient4Cluster();
        boolean flag =false;
        try {
            for(String key : keys){
                jedis.del(key);
            }
            flag =true;
            return flag;
        } catch (Exception e) {
            throw new BRedisException(e);
        } finally {
            RedisFactory.close(jedis);
        }
        
    }

    @Override
    public List<String> multiGet(Collection<String> keys) {
        JedisCluster jedis = getClient4Cluster();
        List<String> reList = new ArrayList<String>();
        try {
            for(String col :keys){
                String reData =jedis.get(col);
                reList.add(reData);
            }
            return reList;
        } catch (Exception e) {
            logger.error(e);
            throw new BRedisException("redis 获取数据出错!", e);

        } finally {
            RedisFactory.close(jedis);
        }
    }

    @Override
    public Long append(String key, String value) {
        JedisCluster jedis = getClient4Cluster();
        Long re =null;
        try {
            re = jedis.append(key, value);
            return re;
        } catch (Exception e) {
            throw new BRedisException(e);
        } finally {
            RedisFactory.close(jedis);
        }
    }


    @Override
    public long expire(String key, int seconds) {
        JedisCluster jedis = getClient4Cluster();
        Long re =null;
        try {
            re = jedis.expire(key, seconds);
            return re;
        } catch (Exception e) {
            throw new BRedisException(e);
        } finally {
            RedisFactory.close(jedis);
        }
    }

    @Override
    public Long incr(String key) {
        JedisCluster jedis = getClient4Cluster();
        Long re =null;
        try {
            re = jedis.incr(key);
            return re;
        } catch (Exception e) {
            throw new BRedisException(e);
        } finally {
            RedisFactory.close(jedis);
        }
    }
    @Override
    public Long incrBy(String key, Long value) {
        JedisCluster jedis = getClient4Cluster();
        Long re =null;
        try {
            re = jedis.incrBy(key, value);
            return re;
        } catch (Exception e) {
            throw new BRedisException(e);
        } finally {
            RedisFactory.close(jedis);
        }
    }

    @Override
    public Long decr(String key) {
        JedisCluster jedis = getClient4Cluster();
        Long re =null;
        try {
            re = jedis.decr(key);
            return re;
        } catch (Exception e) {
            throw new BRedisException(e);
        } finally {
            RedisFactory.close(jedis);
        }
    }

    @Override
    public Long decrBy(String key, long value) {
        JedisCluster jedis = getClient4Cluster();
        Long re =null;
        try {
            re = jedis.decrBy(key, value);
            return re;
        } catch (Exception e) {
            throw new BRedisException(e);
        } finally {
            RedisFactory.close(jedis);
        }
    }
    
}
